clear *

set maxvar 10000
*set matsize 10000

use "${root}/data/processed/final_sample.dta", clear

keep if baseline_sample == 1

********* Perform (robust bias-corrected) RD estimates and produce results table **********

foreach outcome in tot_exp_3y_avg_pc tot_exp_3y_avg_sgdp tot_rev_3y_avg_pc tot_rev_3y_avg_sgdp current_exp_3y_avg_share investments_3y_avg_share personnel_3y_avg_share social_exp_3y_avg_share social_exp_3y_avg_pc health_3y_avg_share education_3y_avg_share welfare_3y_avg_share nonsocial_exp_3y_avg_share housing_3y_avg_share transport_3y_avg_share other_3y_avg_share {

*** (1) baseline (whole sample, average over 3-years, CCT 2014 MSE-optimal bandwidth)

	qui rdrobust res_`outcome' margin_mayor_left , vce(cluster mun_code) all

	local c1_`outcome' : di %9.2f (e(tau_bc))
	local se1_`outcome' : di %9.2f (e(se_tau_rb))
	local pv1_`outcome' : di %9.2f (e(pv_rb))
	local n1_`outcome' = e(N)
	local e1_`outcome' = e(N_h_l) + e(N_h_r)



*** (2) lame-ducks subsample (3-years average)

	qui rdrobust res_`outcome' margin_mayor_left if lame_duck == 1 & baseline==1, vce(cluster mun_code) all

	local c4_`outcome' : di %9.2f (e(tau_bc))
	local se4_`outcome' : di %9.2f (e(se_tau_rb))
	local pv4_`outcome' : di %9.2f (e(pv_rb))
	local n4_`outcome' = e(N)
	local e4_`outcome' = e(N_h_l) + e(N_h_r)


*** (3) Tiebout-below-median subsample (3-years average)

	qui rdrobust res_`outcome' margin_mayor_left if tiebout_median_sample == 1 & baseline_sample == 1, vce(cluster mun_code) all

	local c5_`outcome' : di %9.2f (e(tau_bc))
	local se5_`outcome' : di %9.2f (e(se_tau_rb))
	local pv5_`outcome' : di %9.2f (e(pv_rb))
	local n5_`outcome' = e(N)
	local e5_`outcome' = e(N_h_l) + e(N_h_r)


*** (4) Tiebout-below-75th percentile subsample (3-years average)

	qui rdrobust res_`outcome' margin_mayor_left if tiebout_75th_sample == 1 & baseline_sample == 1, vce(cluster mun_code) all

	local c6_`outcome' : di %9.2f (e(tau_bc))
	local se6_`outcome' : di %9.2f (e(se_tau_rb))
	local pv6_`outcome' : di %9.2f (e(pv_rb))
	local n6_`outcome' = e(N)
	local e6_`outcome' = e(N_h_l) + e(N_h_r)


*** (5) Oil-windfalls subsample (3-years average)

	qui rdrobust res_`outcome' margin_mayor_left if oil_sample == 1 & baseline_sample == 1, vce(cluster mun_code) all

	local c7_`outcome' : di %9.2f (e(tau_bc))
	local se7_`outcome' : di %9.2f (e(se_tau_rb))
	local pv7_`outcome' : di %9.2f (e(pv_rb))
	local n7_`outcome' = e(N)
	local e7_`outcome' = e(N_h_l) + e(N_h_r)


}


* write table
texdoc init "${root}/results/tables/table_aggregate_3y_avg.tex", replace force

tex \caption{RD estimates of the effect of a left-wing mayor - excluding first year of mayor term}

tex \resizebox{\linewidth}{!}{
tex \begin{tabularx}{\linewidth}{l *5{>{\Centering}X}}
tex \toprule

tex 													&        Baseline 				& \multicolumn{4}{c}{Subsamples} 								\\
tex \cmidrule(lr){2-2} \cmidrule(lr){3-6}

tex 													& 								& 	Lame Duck 			& Tiebout $<$ median	 								& Ideology distance $>$ median  						& Oil windfall					\\
tex \midrule

tex \multicolumn{6}{c}{Size of government: overall revenues and expenses} \\
tex \midrule
tex Expenditure per capita 					& `c1_tot_exp_3y_avg_pc'`stars1_tot_exp_3y_avg_pc' 				& `c4_tot_exp_3y_avg_pc'`stars4_tot_exp_3y_avg_pc' 				& `c5_tot_exp_3y_avg_pc'`stars5_tot_exp_3y_avg_pc' 				& `c6_tot_exp_3y_avg_pc'`stars6_tot_exp_3y_avg_pc' 				& `c7_tot_exp_3y_avg_pc'`stars7_tot_exp_3y_avg_pc' 			\\
tex  										& (`se1_tot_exp_3y_avg_pc') 										& (`se4_tot_exp_3y_avg_pc') 										& (`se5_tot_exp_3y_avg_pc') 										& (`se6_tot_exp_3y_avg_pc') 										& (`se7_tot_exp_3y_avg_pc') 						\\
tex Expenditure, \% of GDP 					& `c1_tot_exp_3y_avg_sgdp'`stars1_tot_exp_3y_avg_sgdp' 			& `c4_tot_exp_3y_avg_sgdp'`stars4_tot_exp_3y_avg_sgdp' 			& `c5_tot_exp_3y_avg_sgdp'`stars5_tot_exp_3y_avg_sgdp' 		 	& `c6_tot_exp_3y_avg_sgdp'`stars6_tot_exp_3y_avg_sgdp' 		 	& `c7_tot_exp_3y_avg_sgdp'`stars7_tot_exp_3y_avg_sgdp' 		 	\\
tex  										& (`se1_tot_exp_3y_avg_sgdp') 										& (`se4_tot_exp_3y_avg_sgdp') 										& (`se5_tot_exp_3y_avg_sgdp') 										& (`se6_tot_exp_3y_avg_sgdp') 										& (`se7_tot_exp_3y_avg_sgdp') 						\\
tex Revenue per capita 						& `c1_tot_rev_3y_avg_pc'`stars1_tot_rev_3y_avg_pc' 				& `c4_tot_rev_3y_avg_pc'`stars4_tot_rev_3y_avg_pc' 				& `c5_tot_rev_3y_avg_pc'`stars5_tot_rev_3y_avg_pc' 				& `c6_tot_rev_3y_avg_pc'`stars6_tot_rev_3y_avg_pc' 				& `c7_tot_rev_3y_avg_pc'`stars7_tot_rev_3y_avg_pc' 			\\
tex  										& (`se1_tot_rev_3y_avg_pc') 										& (`se4_tot_rev_3y_avg_pc') 										& (`se5_tot_rev_3y_avg_pc') 										& (`se6_tot_rev_3y_avg_pc') 										& (`se7_tot_rev_3y_avg_pc') 						\\
tex Revenue, \% of GDP 						& `c1_tot_rev_3y_avg_sgdp'`stars1_tot_rev_3y_avg_sgdp' 			& `c4_tot_rev_3y_avg_sgdp'`stars4_tot_rev_3y_avg_sgdp' 			& `c5_tot_rev_3y_avg_sgdp'`stars5_tot_rev_3y_avg_sgdp' 			& `c6_tot_rev_3y_avg_sgdp'`stars6_tot_rev_3y_avg_sgdp' 			& `c7_tot_rev_3y_avg_sgdp'`stars7_tot_rev_3y_avg_sgdp' 			\\
tex  										& (`se1_tot_rev_3y_avg_sgdp') 										& (`se4_tot_rev_3y_avg_sgdp') 										& (`se5_tot_rev_3y_avg_sgdp') 										& (`se6_tot_rev_3y_avg_sgdp') 										& (`se7_tot_rev_3y_avg_sgdp') 					 	\\
tex \midrule
tex \multicolumn{6}{c}{Allocation of resources: budget categories (\% of total expenditure)} \\
tex \midrule
tex Current Expenditure 					& `c1_current_exp_3y_avg_share'`stars1_current_exp_3y_avg_share' 	& `c4_current_exp_3y_avg_share'`stars4_current_exp_3y_avg_share' 	& `c5_current_exp_3y_avg_share'`stars5_current_exp_3y_avg_share' 	& `c6_current_exp_3y_avg_share'`stars6_current_exp_3y_avg_share' 	& `c7_current_exp_3y_avg_share'`stars7_current_exp_3y_avg_share' 		\\
tex  										& (`se1_current_exp_3y_avg_share') 								& (`se4_current_exp_3y_avg_share') 								& (`se5_current_exp_3y_avg_share') 								& (`se6_current_exp_3y_avg_share') 								& (`se7_current_exp_3y_avg_share') 					\\
tex \multicolumn{6}{l}{of which:} \\
tex \hspace{0.20cm} Personnel 								& `c1_personnel_3y_avg_share'`stars1_personnel_3y_avg_share' 		& `c4_personnel_3y_avg_share'`stars4_personnel_3y_avg_share' 		& `c5_personnel_3y_avg_share'`stars5_personnel_3y_avg_share' 		& `c6_personnel_3y_avg_share'`stars6_personnel_3y_avg_share' 		& `c7_personnel_3y_avg_share'`stars7_personnel_3y_avg_share' 		\\
tex  										& (`se1_personnel_3y_avg_share') 									& (`se4_personnel_3y_avg_share') 									& (`se5_personnel_3y_avg_share') 									& (`se6_personnel_3y_avg_share') 									& (`se7_personnel_3y_avg_share') 						\\
tex Public Investment 						& `c1_investments_3y_avg_share'`stars1_investments_3y_avg_share' 	& `c4_investments_3y_avg_share'`stars4_investments_3y_avg_share' 	& `c5_investments_3y_avg_share'`stars5_investments_3y_avg_share' 	& `c6_investments_3y_avg_share'`stars6_investments_3y_avg_share' 	& `c7_investments_3y_avg_share'`stars7_investments_3y_avg_share' 		\\
tex  										& (`se1_investments_3y_avg_share') 								& (`se4_investments_3y_avg_share') 								& (`se5_investments_3y_avg_share') 								& (`se6_investments_3y_avg_share') 								& (`se7_investments_3y_avg_share') 					\\
tex \midrule
tex \multicolumn{6}{c}{Allocation of resources: functional categories (\% of total expenditure)} \\
tex \midrule
tex Social Expenditures 					& `c1_social_exp_3y_avg_share'`stars1_social_exp_3y_avg_share' 	& `c4_social_exp_3y_avg_share'`stars4_social_exp_3y_avg_share' 	& `c5_social_exp_3y_avg_share'`stars5_social_exp_3y_avg_share' 	& `c6_social_exp_3y_avg_share'`stars6_social_exp_3y_avg_share' 	& `c7_social_exp_3y_avg_share'`stars7_social_exp_3y_avg_share' 		\\
tex  										& (`se1_social_exp_3y_avg_share') 									& (`se4_social_exp_3y_avg_share') 						 			& (`se5_social_exp_3y_avg_share') 						 			& (`se6_social_exp_3y_avg_share') 						 			& (`se7_social_exp_3y_avg_share') 						\\
tex \multicolumn{6}{l}{of which:} \\
tex \hspace{0.20cm} Health \& sanitation 	& `c1_health_3y_avg_share'`stars1_health_3y_avg_share' 			& `c4_health_3y_avg_share'`stars4_health_3y_avg_share' 			& `c5_health_3y_avg_share'`stars5_health_3y_avg_share' 			& `c6_health_3y_avg_share'`stars6_health_3y_avg_share' 			& `c7_health_3y_avg_share'`stars7_health_3y_avg_share' 			\\
tex  										& (`se1_health_3y_avg_share') 										& (`se4_health_3y_avg_share') 										& (`se5_health_3y_avg_share') 										& (`se6_health_3y_avg_share') 										& (`se7_health_3y_avg_share') 						\\
tex \hspace{0.20cm} Education \& culture 	& `c1_education_3y_avg_share'`stars1_education_3y_avg_share' 		& `c4_education_3y_avg_share'`stars4_education_3y_avg_share' 		& `c5_education_3y_avg_share'`stars5_education_3y_avg_share' 		& `c6_education_3y_avg_share'`stars6_education_3y_avg_share' 		& `c7_education_3y_avg_share'`stars7_education_3y_avg_share' 		\\
tex  										& (`se1_education_3y_avg_share') 									& (`se4_education_3y_avg_share') 									& (`se5_education_3y_avg_share') 									& (`se6_education_3y_avg_share') 									& (`se7_education_3y_avg_share') 						\\
tex \hspace{0.20cm} Social welfare 			& `c1_welfare_3y_avg_share'`stars1_welfare_3y_avg_share' 			& `c4_welfare_3y_avg_share'`stars4_welfare_3y_avg_share' 			& `c5_welfare_3y_avg_share'`stars5_welfare_3y_avg_share' 			& `c6_welfare_3y_avg_share'`stars6_welfare_3y_avg_share' 			& `c7_welfare_3y_avg_share'`stars7_welfare_3y_avg_share' 			\\
tex  										& (`se1_welfare_3y_avg_share') 									& (`se4_welfare_3y_avg_share') 									& (`se5_welfare_3y_avg_share') 									& (`se6_welfare_3y_avg_share') 									& (`se7_welfare_3y_avg_share') 						\\

tex \multicolumn{6}{l}{Other expenditures:} \\
tex  \hspace{0.20cm} Housing 				& `c1_housing_3y_avg_share'`stars1_housing_3y_avg_share' 			& `c4_housing_3y_avg_share'`stars4_housing_3y_avg_share' 			& `c5_housing_3y_avg_share'`stars5_housing_3y_avg_share' 			& `c6_housing_3y_avg_share'`stars6_housing_3y_avg_share' 			& `c7_housing_3y_avg_share'`stars7_housing_3y_avg_share' 			\\
tex  										& (`se1_housing_3y_avg_share') 									& (`se4_housing_3y_avg_share') 									& (`se5_housing_3y_avg_share') 									& (`se6_housing_3y_avg_share') 									& (`se7_housing_3y_avg_share') 						\\
tex  \hspace{0.20cm} Transportation 		& `c1_transport_3y_avg_share'`stars1_transport_3y_avg_share' 		& `c4_transport_3y_avg_share'`stars4_transport_3y_avg_share' 		& `c5_transport_3y_avg_share'`stars5_transport_3y_avg_share' 		& `c6_transport_3y_avg_share'`stars6_transport_3y_avg_share' 		& `c7_transport_3y_avg_share'`stars7_transport_3y_avg_share' 		\\
tex  										& (`se1_transport_3y_avg_share') 									& (`se4_transport_3y_avg_share') 									& (`se5_transport_3y_avg_share') 									& (`se6_transport_3y_avg_share') 									& (`se7_transport_3y_avg_share') 						\\
tex  \hspace{0.20cm} Other 					& `c1_other_3y_avg_share'`stars1_other_3y_avg_share' 				& `c4_other_3y_avg_share'`stars4_other_3y_avg_share' 				& `c5_other_3y_avg_share'`stars5_other_3y_avg_share' 				& `c6_other_3y_avg_share'`stars6_other_3y_avg_share' 				& `c7_other_3y_avg_share'`stars7_other_3y_avg_share' 			\\
tex  										& (`se1_other_3y_avg_share') 										& (`se4_other_3y_avg_share') 										& (`se5_other_3y_avg_share') 										& (`se6_other_3y_avg_share') 										& (`se7_other_3y_avg_share') 						\\
tex \midrule

tex Social Expenditures per capita 			& `c1_social_exp_3y_avg_pc'`stars1_social_exp_3y_avg_pc' 			& `c4_social_exp_3y_avg_pc'`stars4_social_exp_3y_avg_pc' 			& `c5_social_exp_3y_avg_pc'`stars5_social_exp_3y_avg_pc' 			& `c6_social_exp_3y_avg_pc'`stars6_social_exp_3y_avg_pc' 			& `c7_social_exp_3y_avg_pc'`stars7_social_exp_3y_avg_pc' 			\\
tex  										& (`se1_social_exp_3y_avg_pc') 									& (`se4_social_exp_3y_avg_pc') 						 			& (`se5_social_exp_3y_avg_pc') 						 			& (`se6_social_exp_3y_avg_pc') 						 			& (`se7_social_exp_3y_avg_pc') 						\\
tex \bottomrule
tex Observations (all) 						&  `n1_tot_exp_3y_avg_pc'    										& `n4_tot_exp_3y_avg_pc'  											& `n5_tot_exp_3y_avg_pc'  											& `n6_tot_exp_3y_avg_pc'  											& `n7_tot_exp_3y_avg_pc'  		\\
tex Observations (effective)				&  `e1_tot_exp_3y_avg_pc'										& `e4_tot_exp_3y_avg_pc'										& `e5_tot_exp_3y_avg_pc'   										& `e6_tot_exp_3y_avg_pc'										& `e7_tot_exp_3y_avg_pc'   	\\
tex \bottomrule
tex \end{tabularx}}

texdoc close
